package com.fire.eyes.controller;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.DateTimeUtil;
import com.fire.eyes.model.*;
import com.fire.eyes.service.INoticeService;
import com.fire.eyes.service.IUserLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *
 * @author yxt
 *
 */
@RestController
@RequestMapping("/club")
public class JudgeNoticeMsgController {
    private static final Logger logger = LoggerFactory.getLogger(JudgeNoticeMsgController.class);
    @Autowired
    private IUserLoginService clubUserLoginService;
//    @Autowired
//    private NoticeBusinessService noticeBusinessService;
    @Autowired
    private INoticeService noticeMsgMapperService;

    @RequestMapping(value = "judgenoticemsg", method = RequestMethod.POST)
    public AjaxResult denewNoticeMsg(@RequestBody String json) {
        logger.info("-----22判断用户消息接口    start-----");
        AjaxResult result = new AjaxResult();
        JudgeNoticeMsgReq req = JSON.parseObject(json, JudgeNoticeMsgReq.class);
        logger.info("获取到的json数据{}", req);
        // 解析token，这里需要重新写
        String phone = "";
        int userId = 1;
        // 通过用户id 查询上次登陆记录
        UserLoginModel clubUserlogin = clubUserLoginService.selectOne(new EntityWrapper<UserLoginModel>().eq("user_id","userId"));//这里需要重新封装，clubUserLoginService.queryUserLogin(userId);
        if (clubUserlogin != null) {
            clubUserlogin.setLastloginTime(clubUserlogin.getLoginTime());
            clubUserlogin.setLoginTime(DateTimeUtil.getCurrentDate());
            clubUserLoginService.updateById(clubUserlogin);
            // 查询上次登陆后的所有新增记录
            clubUserlogin.setLastloginTime(clubUserlogin.getLastloginTime());
            clubUserlogin.setLoginTime(clubUserlogin.getLoginTime());
            clubUserlogin.setUserId(userId);
            List<NoticeModel> list = noticeMsgMapperService.queryAllNotice(clubUserlogin);
            result=checkNotice(list);

        } else {// 如果没有登陆过，需要查询所有本次登陆之前的所有消息
            UserLoginModel clubUserlogin2 = new UserLoginModel();
            clubUserlogin2.setLastloginTime(DateTimeUtil.getCurrentDate());
            clubUserlogin2.setLoginTime(DateTimeUtil.getCurrentDate());
            clubUserlogin2.setLoginNumber(1);
            clubUserlogin2.setUserId(userId);
            boolean b = clubUserLoginService.insert(clubUserlogin2);
            // 执行查询
            List<NoticeModel> list2 = noticeMsgMapperService.queryFirstLoginNotice(clubUserlogin2);
            result=checkNotice(list2);
        }

        return result;
    }
    public AjaxResult checkNotice( List<NoticeModel> list){
        AjaxResult result=new AjaxResult();
        if (list.size() > 0) {
//                noticeBusinessService.checkNotice(list2, phone, userId);
            result.setStatus("0");
            result.setMessage("消息查询存入缓存成功");
        } else {
            result.setStatus("1");
            result.setMessage("该用户暂无新消息");
        }

        return result;
    }

}
